package com.zs.propertymanage.mapper;

import com.zs.propertymanage.dataObj.entity.Log;
import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
public interface LogMapper {

    // 查询日志
    @Select("select * from log where requestDate between #{startDate} and #{endDate} order by requestDate desc limit #{start},#{pageSize}")
    List<Log> getLog(Integer start, Integer pageSize, LocalDateTime startDate, LocalDateTime endDate);

    // 查询日志总数
    @Select("select count(*) from log where requestDate between #{startDate} and #{endDate}")
    Integer getLogCount(LocalDateTime startDate, LocalDateTime endDate);

    // 插入新日志
    @Insert("insert into log " +
            "(id, moduleName, methodName, requestUrl, requestMethod, requestIp, requestDate, userId, userName, userIdentity)" +
            "values ( #{id}, #{moduleName}, #{methodName}, " +
            "#{requestUrl}, #{requestMethod}, #{requestIp}, #{requestDate}, " +
            "#{userId}, #{userName}, #{userIdentity} )")
    void insertLog(Log log);

    // 清除7天以外的Get请求日志、登录/退出日志
    @Delete("delete from log where requestDate <  NOW() - INTERVAL 7 DAY and (requestMethod = 'GET' or requestUrl = '/user/login')")
    void deleteOldLog();
}
